package com.example.kehoobackend.controller;


import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.domain.Question;
import com.example.kehoobackend.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Validated
@CrossOrigin
@RestController
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 查询所有问题
     *
     * @param current_id 当前用户id
     * @return CommonResponse<List < Question>>
     */
    @GetMapping("/question")
    public CommonResponse<List<Question>> getAllQuestion(
            @RequestParam("current_id") Integer current_id) {
        if (current_id == 0)
            return questionService.getAllQuestion1();
        return questionService.getAllQuestion(current_id);
    }

    /**
     * 根据话题id查询问题
     *
     * @param current_id 当前用户id
     * @param topic_id 话题id
     * @return CommonResponse<List < Question>>
     */
    @GetMapping("/question/topic_id")
    public CommonResponse<List<Question>> getQuestionByTopicId(
            @RequestParam("current_id") Integer current_id,
            @RequestParam("topic_id") Integer topic_id) {
        return questionService.getQuestionByTopicId(current_id,topic_id);
    }

    /**
     * 根据标题或内容查询问题
     *
     * @param current_id 当前用户id
     * @param keyword 关键字
     * @return CommonResponse<List < Question>>
     */
    @GetMapping("/question/keyword")
    public CommonResponse<List<Question>> getQuestionByKeyword(
            @RequestParam("current_id") Integer current_id,
            @RequestParam("keyword") String keyword) {
        return questionService.getQuestionByKeyword(current_id,keyword);
    }

    /**
     * 根据传入问题类型和排序类别返回问题列表
     *
     * @param current_id 当前用户id
     * @param order_type 排序类别（latest,hot）
     * @param user_id    用户id(空时默认，有值时查用户关注的)
     * @param start      开始位置
     * @param limit      限制条数
     * @return CommonResponse<List < Question>>
     */
    @GetMapping("/question/list")
    public CommonResponse<List<Question>> getQuestionByOrderType(
            @RequestParam("current_id") Integer current_id,
            @RequestParam("order_type") String order_type,
            @RequestParam("user_id") Integer user_id,
            @RequestParam("start") Integer start,
            @RequestParam("limit") Integer limit) {
        if (user_id.equals(-1)) return questionService.getQuestionForHome(current_id,order_type, start, limit);
        else
            return questionService.getQuestionForCollect(current_id,order_type, user_id, start, limit);
    }

    /**
     * 根据问题点赞数降序返回问题列表
     *
     * @param current_id 当前用户id
     * @param start 开始位置
     * @param limit 限制条数
     * @return CommonResponse<List < Question>>
     */
    @GetMapping("/question/like/desc")
    public CommonResponse<List<Question>> getQuestionByLikeDesc(
            @RequestParam("current_id") Integer current_id,
            @RequestParam("start") Integer start,
            @RequestParam("limit") Integer limit) {
        return questionService.getQuestionByLikeDesc(current_id,start, limit);
    }

    /**
     * 根据用户ID查询问题
     *
     * @param user_id 用户id
     * @return CommonResponse<List < Question>>
     */
    @GetMapping("/question/user_id")
    public CommonResponse<List<Question>> getQuestionByUserId(
            @RequestParam("user_id") Integer user_id) {
        return questionService.getQuestionByUserId(user_id);
    }

    /**
     * 查询用户是否对某个问题点赞
     *
     * @param user_id 用户id
     * @param question_id 问题id
     * @return CommonResponse<Boolean>
     */
    @GetMapping("/question/like")
    public CommonResponse<Boolean> getQuestionLike(
            @RequestParam("user_id") Integer user_id,
            @RequestParam("question_id") Integer question_id) {
        return questionService.isLike(user_id, question_id)
                ? CommonResponse.createForSuccess("用户已点赞", true)
                : CommonResponse.createForSuccess("用户未点赞", false);
    }

    /**
     * 根据问题id查询问题详情
     *
     * @param question_id 问题id
     * @return CommonResponse<Question>
     */
    @GetMapping("/question/detail")
    public CommonResponse<Question> getQuestionDetail(
            @RequestParam("question_id") Integer question_id) {
        return questionService.getQuestionDetail(question_id);
    }

    /**
     * 根据问题id查询提出问题的用户id
     *
     * @param question_id 问题id
     * @return CommonResponse<Integer>
     */
    @GetMapping("/question/user/id")
    public CommonResponse<Integer> getUserIdByQuestionId(
            @RequestParam("question_id") Integer question_id) {
        return CommonResponse.createForSuccess("用户id",questionService.getUserIdByQuestionId(question_id));
    }




    /**
     * 新增问题
     *
     * @param question      问题
     * @param topic_id_list 话题id
     * @return CommonResponse<Question>
     */
    @PostMapping("/question/topic_id/list")
    public CommonResponse<Question> addQuestion(
            @RequestBody Question question,
            @RequestParam("topic_id_list") String topic_id_list) {
        // 以空格为分割符分割字符串
        String[] topic_id_list_str = topic_id_list.split(" ");
        // 将字符串数组转换为Integer数组
        List<Integer> list = new ArrayList<>();
        for (String s : topic_id_list_str) {
            list.add(Integer.parseInt(s));
        }
        // 输出测试
        for (Integer integer : list) {
            System.out.println(integer);
        }
        return CommonResponse.createForSuccess("新增问题",questionService.addQuestion(question,list));
    }


    /**
     * 点赞问题
     * @param question_id 问题id
     * @param user_id 用户id
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/question/like")
    public CommonResponse<Boolean> likeQuestion(
            @RequestParam("question_id") Integer question_id,
            @RequestParam("user_id") Integer user_id) {
        return questionService.like(question_id, user_id)
                ? CommonResponse.createForSuccess("点赞问题成功", true)
                : CommonResponse.createForSuccess("点赞问题失败", false);
    }


    /**
     * 删除问题
     *
     * @param question_id 问题id
     * @return CommonResponse<Boolean>
     */
    @DeleteMapping("/question/{question_id}")
    public CommonResponse<Boolean> deleteQuestion(
            @PathVariable("question_id") Integer question_id) {
        return questionService.deleteQuestion(question_id)
                ? CommonResponse.createForSuccess("删除问题成功", true)
                : CommonResponse.createForSuccess("删除问题失败", false);
    }

    /**
     * 取消点赞
     *
     * @param question_id 问题id
     * @param user_id 用户id
     * @return CommonResponse<Boolean>
     */
    @DeleteMapping("/question/like")
    public CommonResponse<Boolean> cancelLikeQuestion(
            @RequestParam("question_id") Integer question_id,
            @RequestParam("user_id") Integer user_id) {
        return questionService.cancelLike(question_id, user_id)
                ? CommonResponse.createForSuccess("取消点赞成功", true)
                : CommonResponse.createForSuccess("取消点赞失败", false);
    }

}
